package com.zlzxm.shoesmarket.view.adapter;

import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * CREATE BY zlzxm ON 4/13/21
 */
public class ADVipInfo extends BaseQuickAdapter<ADVipInfo.EntityVipInfo, BaseViewHolder> {


    private int selectPosition = -1;


    public void setSelectPosition(int position){

        if(selectPosition == position){

            return;
        }


        if(selectPosition!=-1){

            EntityVipInfo old = getData().get(selectPosition);
            EntityVipInfo newEntity = getData().get(position);

            old.setSelect(false);

            newEntity.setSelect(true);

            notifyDataSetChanged();

            selectPosition = position;

        }else {

            EntityVipInfo newEntity = getData().get(position);


            newEntity.setSelect(true);

            notifyDataSetChanged();

            selectPosition = position;


        }



    }


    public int getSelectPosition() {
        return selectPosition;
    }

    public ADVipInfo(@Nullable List<EntityVipInfo> data) {
        super(R.layout.item_vip_info, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, EntityVipInfo entity) {

        holder.itemView.setSelected(entity.isSelect());

        holder.setText(R.id.tvTitle,entity.getName());


        holder.setText(R.id.tvDesc,entity.getDesc());


        TextView tvTitle = holder.getView(R.id.tvTitle);

        if(entity.isSelect()) {

            tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        }else {

            tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        }


        SpannableString price = new SpannableString(Constant.Character.CNY+entity.getPrice());

        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(0.54f);

        price.setSpan(sizeSpan,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        holder.setText(R.id.tvPrice,price);

        String realPrice = entity.getRealPrice();
        SpannableString spanRealPrice = new SpannableString(realPrice);
        StrikethroughSpan span = new StrikethroughSpan();
        spanRealPrice.setSpan(span, 0, realPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.setText(R.id.tvRealPrice,spanRealPrice);

    }



    public static class EntityVipInfo {

        private boolean isSelect;

        private String name;

        private String price;

        private String realPrice;

        private String desc;

        public EntityVipInfo( String name, String price, String realPrice, String desc) {
            this.name = name;
            this.price = price;
            this.realPrice = realPrice;
            this.desc = desc;
        }

        boolean isSelect() {
            return isSelect;
        }

        void setSelect(boolean select) {
            isSelect = select;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getRealPrice() {
            return Constant.Character.CNY+realPrice;
        }

        public void setRealPrice(String realPrice) {
            this.realPrice = realPrice;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
